package object;

import java.util.Objects;

public class Cat{
	private String name;
	private String color;
	
	public Cat() {
		super();
	}
	public Cat(String name, String color) {
		super();
		this.name = name;
		this.color = color;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	@Override
	public String toString() {
		return "Cat [name=" + name + ", color=" + color + "]";
	}
	
	// equals和hashCode返回对象在内存地址
	@Override
	public int hashCode() {
		return Objects.hash(color, name);
	}
	@Override
	public boolean equals(Object obj) {
		// 如果当前对象和参数是指向同一个地址直接返回true
		if (this == obj)
			return true;
		// 如果参数是null,返回false
		if (obj == null)
			return false;
		// 对象类型和参数类型不一样就别比了
		if (this.getClass() != obj.getClass())
			return false;
		// 强制类型转成猫
		Cat other = (Cat) obj;
		// 如果颜色一样，name一样就返回true,否则有一个不同就返回false
		return Objects.equals(color, other.color) && Objects.equals(name, other.name);
	}
	
	// final, try-catch-finally
	@Override
	protected void finalize() throws Throwable {
		System.out.println("当对象的堆空间马上被销毁之前执行的..");
	}
}
